/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.systemManag.controller;

import com.koron.workflow.systemManag.bean.EnumQueryBean;
import com.koron.workflow.systemManag.service.EnumService;
import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(value = "enum", description = "数据字典")
@RestController
@RequestMapping("/enum")
public class EnumController {
    private static final Logger logger = LoggerFactory.getLogger(EnumController.class);

    @Autowired
    private EnumService service;

    @ApiOperation("数据字典分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典组编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "字典组名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryForList.htm")
    public MessageBean queryForList(@ApiIgnore String code, @ApiIgnore String name,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null) {
            page = page - 1;
            if (page < 0) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    code, name, page * pageSize, pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询数据字典失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("根据字典组编号查询数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典组编号", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/queryForObject.htm")
    public MessageBean queryForObject(@ApiIgnore String code) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            EnumQueryBean bean = ADOConnection.runTask(service, "queryForObject", EnumQueryBean.class, code);
            msg.setData(bean);
        } catch (Exception e) {
            logger.error("根据字典组编号查询数据字典失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("根据字典组编号查询字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典组编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "filterKeys", value = "要过滤的字典key,多个以逗号分隔", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/querySelectByCode.htm")
    public MessageBean querySelectByCode(@ApiIgnore String code, @ApiIgnore String filterKeys) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            List list = ADOConnection.runTask(service, "querySelectByCode", List.class, code, filterKeys);
            msg.setData(list);
        } catch (Exception e) {
            logger.error("根据字典组编号查询数据字典失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("获取所有数据字典信息")
    @GetMapping("/queryAllEnum.htm")
    public MessageBean queryAllEnum() {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            List<Map> list = ADOConnection.runTask(service, "queryAllEnum", List.class);
            msg.setData(list);
        } catch (Exception e) {
            logger.error("根据字典组编号查询数据字典失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("保存数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典组编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "字典组名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "itemBeans", value = "字典组下各字典信息(json数据格式，['key':'key','value':'value','sn':'sn'])",
                    required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping("/save.htm")
    public MessageBean save(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid EnumQueryBean bean) {
        return ADOConnection.runTask(service, "save", MessageBean.class, user, bean);
    }

    @ApiOperation("修改数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldCode", value = "历史字典组编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "字典组编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "字典组名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "itemBeans", value = "字典组下各字典信息(json数据格式，['key':'key','value':'value','sn':'sn'])",
                    required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping("/update.htm")
    public MessageBean update(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid EnumQueryBean bean) {
        return ADOConnection.runTask(service, "update", MessageBean.class, user, bean.getOldCode(), bean);
    }

    @ApiOperation("根据字典组编号删除数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典组编号", required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String code) {
        return ADOConnection.runTask(service, "delete", MessageBean.class, code);
    }
}
